local hx__jianying = fk.CreateSkill {
  name = "hx__jianying",
}


hx__jianying:addEffect("viewas", {
  max_phase_use_time = 1,
  prompt = "#hx__jianying-active",
  interaction = function(self, player)
    local all_names = Fk:getAllCardNames("b")
    return UI.CardNameBox {
      choices = player:getViewAsCardNames(hx__jianying.name, all_names),
      all_choices = all_names,
      default_choice = "AskForCardsChosen",
    }
  end,
  handly_pile = true,
  card_filter = function(self, player, to_select, selected)
    return #selected == 0 and Fk.all_card_types[self.interaction.data] ~= nil
  end,
  view_as = function(self, player, cards)
    if Fk.all_card_types[self.interaction.data] == nil or #cards ~= 1 then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(cards)

    local suitstrings = {"spade", "heart", "club", "diamond"}
    local suits = {Card.Spade, Card.Heart, Card.Club, Card.Diamond}
    local colors = {Card.Black, Card.Red, Card.Black, Card.Diamond}
    local suit = player:getMark("hx__jianying_suit-phase")
    if table.contains(suitstrings, suit) then
      card.suit = suits[table.indexOf(suitstrings, suit)]
      card.color = colors[table.indexOf(suitstrings, suit)]
    end

    card.skillName = self.name
    return card
  end,
  before_use = function(self, player, use)
    player.room:setPlayerMark(player, "hx__jianying_used-phase", 1)
  end,
  enabled_at_play = function(self, player)
    return player:getMark("hx__jianying_used-phase") == 0
  end,
})


hx__jianying:addEffect(fk.CardUsing, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return data.from == player and player.phase == Player.Play and
      (data.extra_data or {}).hx__jianying_triggerable and player:hasSkill(hx__jianying.name)
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(1, hx__jianying.name)
  end,
})

hx__jianying:addEffect(fk.CardUsing, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__jianying.name, true) and player.phase == Player.Play
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if data.card:getSuitString() == player:getMark("hx__jianying_suit-phase") or
        (data.card.number == player:getMark("hx__jianying_number-phase") and data.card.number ~= 0) then
      data.extra_data = data.extra_data or {}
      data.extra_data.hx__jianying_triggerable = true
    end
    if data.card.suit == Card.NoSuit then
      room:setPlayerMark(player, "hx__jianying_suit-phase", 0)
    else
      room:setPlayerMark(player, "hx__jianying_suit-phase", data.card:getSuitString())
    end
    room:setPlayerMark(player, "hx__jianying_number-phase", data.card.number)
    room:setPlayerMark(player, "@hx__jianying_record-phase", {data.card:getSuitString(true), data.card:getNumberStr()})
  end,
})

hx__jianying:addLoseEffect(function(self, player)
  local room = player.room
  room:setPlayerMark(player, "hx__jianying_used-phase", 0)
  room:setPlayerMark(player, "hx__jianying_suit-phase", 0)
  room:setPlayerMark(player, "hx__jianying_number-phase", 0)
  room:setPlayerMark(player, "@hx__jianying_record-phase", 0)
end)



return hx__jianying